//
//  DHCart_WashingCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/22.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHCart_WashingCell.h"
#import "DHCart_WashingItemCell.h"

@interface DHCart_WashingCell()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *arr;
@property (strong, nonatomic) NSArray *selectedArr;

@end

@implementation DHCart_WashingCell

- (NSArray *)arr{
    if (!_arr) {
        _arr = [NSArray array];
    }
    return _arr;
}

- (NSArray *)selectedArr{
    if (!_selectedArr) {
        _selectedArr = [NSArray array];
    }
    return _selectedArr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHCart_WashingItemCell" bundle:nil] forCellReuseIdentifier:kDHCart_WashingItemCell];
}

- (void)updateCellArr:(NSArray *)arr selectedArr:(NSArray *)selectedArr{
    self.arr = arr;
    self.selectedArr = selectedArr;
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHCart_WashingItemCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCart_WashingItemCell forIndexPath:indexPath];
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    [cell updateCell:dic];
    if ([self.selectedArr containsObject:dic]) {
        [cell.selectedButton setImage:[UIImage imageNamed:@"cart_selected.png"] forState:UIControlStateNormal];
        cell.itemSelected = YES;
    }else{
        [cell.selectedButton setImage:[UIImage imageNamed:@"cart_unselected.png"] forState:UIControlStateNormal];
        cell.itemSelected = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
        [[DHNetAPIClient shareDHNetAPIClient] request_cart_deleteWithParams:@{@"cart_ids":@[[dic objectForKey:@"cart_id"]]} andBlock:^(id data, NSError *error) {
            [self routerEventWithName:@"leased_delete" userInfo:@{@"index":@(indexPath.row)}];
            //            [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
            [[GlobalManager shareGlobalManager] refreshCartNumberAndBlock:^(id data, NSError *error) {
            }];
        }];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
